package com.ymatou.iapi.topicrecbizer.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.topicrecbizer.parameter.RelatedProductBean;
import com.ymatou.iapi.topicrecbizer.service.RelatedProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

public class Ts_RelatedProduct {
	private static RelatedProductBean relatedproductBean;
	private static RelatedProductCall relatedproductCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品Topic相关主题推荐");
	}

	@Before
	public void caseUp() {
		relatedproductBean = new RelatedProductBean();
		relatedproductCall = new RelatedProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(relatedproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RelatedProduct_001() {
		Logger.start(true,"商品Topic相关主题推荐-检查接口返回公共信息 ");
		try {
			//设置参数
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
			String timenow = df.format(new Date());// new Date()为获取当前系统时间
			int userid = 123456789;
			String requestid="111111";
			String score = "tpscore";
			String module="product_topic_list";
			
			relatedproductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			relatedproductBean.setSort("desc("+score+")");
			relatedproductBean.setAppkey("1");
			relatedproductBean.setRequestid(requestid);
			relatedproductBean.setModule(module);
			relatedproductBean.setPlatform("appbuyer");
			relatedproductBean.setLimit("0,10");
			
			relatedproductCall.setData(relatedproductBean);
			relatedproductCall.callService();
			
			
			Logger.verifyEquals("OK",relatedproductCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(relatedproductCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, relatedproductCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, relatedproductCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, relatedproductCall.getCommonInfo("requestid"), "验证requestid值是否为所传值");
			Logger.verifyNotNull(relatedproductCall.getCommonInfo("hostip"), "验证是否有返回服务器ip");
			Logger.verifyEquals(score, relatedproductCall.getCommonInfo("algorithm"), "验证返回排序方式是否与所传值相同");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RelatedProduct_002() {
		Logger.start(true,"商品Topic相关主题推荐-检查接口返回数据，确保不会重复 ");
		try {
			//设置参数
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
			String timenow = df.format(new Date());// new Date()为获取当前系统时间
			int userid = 123456789;
			String requestid="111111";
			String score = "tpscore";
			String module="product_topic_list";
			
			relatedproductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			relatedproductBean.setSort("desc("+score+")");
			relatedproductBean.setAppkey("1");
			relatedproductBean.setRequestid(requestid);
			relatedproductBean.setModule(module);
			relatedproductBean.setPlatform("appbuyer");
			relatedproductBean.setLimit("0,10");
			
			relatedproductCall.setData(relatedproductBean);
			relatedproductCall.callService();
			
			JSONArray hitsAry = relatedproductCall.getHits();

			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);

				String topicId = json.getString("topicId");

				if (list.contains(topicId) == true) {

					Logger.verifyEquals(false, list.contains(topicId), "验证第"+ i + "条返回数据的topicId存在重复值" + topicId);

				} else {
					Logger.verifyEquals(false, list.contains(topicId), "验证第"+ i + "条返回数据的topicId不存在重复值" + topicId);

					list.add(topicId);

				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RelatedProduct_003() {
		Logger.start(true,"商品Topic相关主题推荐-验证hits里的返回字段是否齐全 ");
		try {
			//设置参数
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
			String timenow = df.format(new Date());// new Date()为获取当前系统时间
			int userid = 123456789;
			String requestid="111111";
			String score = "tpscore";
			String module="product_topic_list";
			
			relatedproductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			relatedproductBean.setSort("desc("+score+")");
			relatedproductBean.setAppkey("1");
			relatedproductBean.setRequestid(requestid);
			relatedproductBean.setModule(module);
			relatedproductBean.setPlatform("appbuyer");
			relatedproductBean.setLimit("0,10");
			
			relatedproductCall.setData(relatedproductBean);
			relatedproductCall.callService();
			
			Logger.verifyEquals("OK", relatedproductCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = relatedproductCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("buyReason") , "验证第"+i+"条记录返回结果中有buyReason购买理由这个key");
			  Logger.verifyEquals(true,infoAry.has("topicId") , "验证第"+i+"条记录返回结果中有topicId主题id这个key");
			  Logger.verifyEquals(true,infoAry.has("productIds") , "验证第"+i+"条记录返回结果中有productIds值这个key");
			  Logger.verifyEquals(true,infoAry.has("showTitle") , "验证第"+i+"条记录返回结果中有showTitle值这个key");
			  Logger.verifyEquals(true, infoAry.has("userNum"), "验证第"+i+"条记录返回结果中有userNum值这个key");
			  Logger.verifyEquals(true, infoAry.has("stockNum"), "验证第"+i+"条记录返回结果中有stockNum值这个key");
			  Logger.verifyEquals(true, infoAry.has("picUrl"), "验证第"+i+"条记录返回结果中有picUrl值这个key");
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	/*@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RelatedProduct_004() {
		Logger.start(true,"商品Topic相关主题推荐-返回推荐数据的有效的结束时间大于当期那时间 ");
		try {
			//设置参数
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
			String timenow = df.format(new Date());// new Date()为获取当前系统时间
			int userid = 123456789;
			String requestid="111111";
			String score = "tpscore";
			String module="product_topic_list";
			
			relatedproductBean.setInfo("userid:"+userid+",cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			relatedproductBean.setSort("desc("+score+")");
			relatedproductBean.setAppkey("1");
			relatedproductBean.setRequestid(requestid);
			relatedproductBean.setModule(module);
			relatedproductBean.setPlatform("appbuyer");
			relatedproductBean.setLimit("0,10");
			
			relatedproductCall.setData(relatedproductBean);
			relatedproductCall.callService();
			
			Logger.verifyEquals("OK", relatedproductCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = relatedproductCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);

				String validEndTime = json.getString("validEndTime");
				DateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        
				 Date endtime = time.parse(validEndTime);
				
				//将时间转换成时间戳
				long endTime = endtime.getTime();
				long nowTime =  new Date().getTime();
				
				Logger.verifyEquals(true, endTime>nowTime, "检查第"+i+"条推荐数据有效时间是否大于当前时间");
			
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	*/
	
	
}
